package com.digitalchina.lxnsy.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.InterviewDetailActivity;
import com.digitalchina.lxnsy.api.BusinessExpert;
import com.digitalchina.lxnsy.base.BaseFragment;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.FamousInterviewVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.FamousHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2017/7/31 10:54
 * 修改备注：
 * ================================================
 */
public class PeriodCurrentFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.fm_fi_tv_ai)
    TextView fmFiTvAi;
    @Bind(R.id.fm_fi_tv_time)
    TextView fmFiTvTime;
    @Bind(R.id.fm_fi_tv_read)
    TextView fmFiTvRead;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;


    private int page = 1;
    private static final String LIMIT = "10";
    private String type = "0"; // 0 本期   1 往期
    private String orderType = "1";// 排序类型  1 时间  2 阅读量
    private RecyclerArrayAdapter adapter;
    private List<FamousInterviewVO> voList = new ArrayList<>();


    @Override
    public void initMember() {

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 1, 15, 15));

        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new FamousHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

//        adapter.setMore(R.layout.view_more, this);

//        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                GotoUtil.gotoActivity(getActivity(), InterviewDetailActivity.class, "FamousInterviewVO", adapter.getItem(position));
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_GET_INTER_VIEW_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_INTER_VIEW_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        voList = FastJsonUtil.getListBean(object.toString(), FamousInterviewVO.class);

        if (voList == null)
            return;

//        if (page == 1)  // 刷新
        adapter.clear();

//        if (voList.size() < Integer.parseInt(LIMIT))
//            adapter.stopMore();

        adapter.addAll(voList);

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_famous_interview);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessExpert.getInterViewList(getActivity(), type, orderType, String.valueOf(page), LIMIT, mHandler);
    }


    @OnClick({R.id.fm_fi_tv_ai, R.id.fm_fi_tv_time, R.id.fm_fi_tv_read})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fm_fi_tv_ai:
                orderType = "1";
                setTextColor(0);
                break;
            case R.id.fm_fi_tv_time:
                orderType = "1";
                setTextColor(1);
                break;
            case R.id.fm_fi_tv_read:
                orderType = "2";
                setTextColor(2);
                break;
        }
        onRefresh();
    }

    private void setTextColor(int i) {
        switch (i) {
            case 0:
                fmFiTvAi.setTextColor(getResources().getColor(R.color.app_color));
                fmFiTvTime.setTextColor(getResources().getColor(R.color.colorFontBlack));
                fmFiTvRead.setTextColor(getResources().getColor(R.color.colorFontBlack));
                break;
            case 1:
                fmFiTvAi.setTextColor(getResources().getColor(R.color.colorFontBlack));
                fmFiTvTime.setTextColor(getResources().getColor(R.color.app_color));
                fmFiTvRead.setTextColor(getResources().getColor(R.color.colorFontBlack));
                break;
            case 2:
                fmFiTvAi.setTextColor(getResources().getColor(R.color.colorFontBlack));
                fmFiTvTime.setTextColor(getResources().getColor(R.color.colorFontBlack));
                fmFiTvRead.setTextColor(getResources().getColor(R.color.app_color));
                break;
        }
    }
}
